package sk.octopuss.archery.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import sk.octopuss.archery.dao.AbstractGenericDao;
import sk.octopuss.archery.dao.UserDao;
import sk.octopuss.archery.domain.User;

@Repository
public class UserDaoImpl extends AbstractGenericDao<User> implements UserDao {

	@Override
	public User find(final Long id) {
		final Query query = em.createQuery("Select u FROM User u Where u.id =" + id);
		return (User) query.getSingleResult();
	}

	@Override
	public List<User> findAll() {
		final Query query = em.createQuery("Select u FROM User u");
		return query.getResultList();
	}

	@Override
	public long countAll() {
	      return (Long) em.createQuery("SELECT COUNT(u) FROM User u").getSingleResult();
	}

	@Override
	public long save(final User entity) {
		em.persist(entity);
		return entity.getId();
	}

	@Override
	public void delete(final User entity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User findByUsername(final String username) {
		final Query query = em.createQuery("Select u FROM User u Where u.username='" + username + "'");
		return (User) query.getSingleResult();
	}

	@Override
	public User findByOpenId(final String openIdIdentifier) {
		final Query query = em.createQuery("Select u FROM User u Where u.openID='" + openIdIdentifier + "'");
		return (User) query.getSingleResult();
	}

}
